#TODO xpath标签和属性定位

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')

    def xpath1(self):
        # 定位元素xpath标签和属性
        kw = self.driver.find_element(By.XPATH, '//input[@id="kw"]')
        print(kw.get_attribute('outerHTML'))

        kw = self.driver.find_element(By.XPATH, '//input[@class="s_ipt"]')
        print(kw.get_attribute('outerHTML'))

        kw = self.driver.find_element(By.XPATH, '//*[@maxlength="255"]')
        print(kw.get_attribute('outerHTML'))

    def xpath2(self):
        # xpath层级定位
        ipt = self.driver.find_element(By.XPATH, '//form/span/input')
        print(ipt.get_attribute('outerHTML'))

        ipt = self.driver.find_element(By.XPATH, '//span/input')
        print(ipt.get_attribute('outerHTML'))

        ipt = self.driver.find_element(By.XPATH, '//form/input')
        print(ipt.get_attribute('outerHTML'))

    def xpath3(self):
        # xpath标签索引
        ipt = self.driver.find_element(By.XPATH, '//form/input[2]')
        print(ipt.get_attribute('outerHTML'))

        ipt = self.driver.find_element(By.XPATH, '//form/span[2]')
        print(ipt.get_attribute('outerHTML'))  # 打印标签里面元素内容

    def xpath4(self):
        # xpath逻辑运算索引
        ipt = self.driver.find_element(By.XPATH, '//input[@name="ie" and @type="hidden"]')
        print(ipt.get_attribute('outerHTML'))

    def xpath5(self):
        # xpath模糊匹配
        ipt = self.driver.find_element(By.XPATH, '//form[contains(@name, "rs")]')
        print(ipt.get_attribute('outerHTML'))

    def quit(self):
        # 退出浏览器
        self.driver.quit()


if __name__ == '__main__':
    case = TestCase()
    case.xpath1()
    case.xpath2()
    case.xpath3()
    case.xpath4()
    case.quit()
